package proj.pt.web;

import java.beans.PropertyEditorSupport;

import org.springframework.util.StringUtils;

import proj.pt.model.User;
import proj.pt.services.LogicProcessor;

public class UserEditor extends PropertyEditorSupport {

	private LogicProcessor lp;
	
	public UserEditor(LogicProcessor lp) {
		this.lp = lp;
	}

	public String getAsText() {
		User user = (User)getValue();
		if (user != null) return user.getId().toString();
		else return "";
	}

	public void setAsText(String id) throws IllegalArgumentException {
		if(StringUtils.hasText(id)){
			long lId = Long.parseLong(id);
			User user = lp.getUser(lId);
			setValue(user);
		} else setValue(null);
	}
}
